<?php
namespace app\bews\controller;
use app\bews\controller\Base;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Bewtest extends Base{
	public function icon_list(){
		$lists = Db::table('bew_z_iconfont')->where('status','=',1)->select();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function test_list(){
		if(Request::isPost()){
			$count = Db::table('bew_z_test')->count();
			$page = (int)input('post.page',1);
			$limit = (int)input('post.limit',10);
			$lists = Db::table('bew_z_test')->order('test_id DESC')->page($page,$limit)->select()->each(function($item, $key){
					if($item['test_reference'] == 1){
						$item['test_reference'] = '开启';
					}else{
						$item['test_reference'] = '关闭';
					}
					$item['test_time'] = date('Y-m-d H:i:s',$item['test_time']);
					return $item;
				});
			$this->returnCode(0,$lists,$count);
		}else{
			return View::fetch();
		}
	}
	public function test_add(){
		if(Request::isPost()){
			$data['test_input'] = input('post.test_input');
			$data['test_reference'] = input('post.test_reference');
			$data['test_time'] = input('post.test_time');
			if(!empty($data['test_time'])){
				$data['test_time'] = strtotime($data['test_time']);
			}
			$data['test_data'] = input('post.test_data');
			$data['test_datatime'] = input('post.test_datatime');
			$data['test_img'] = input('post.test_img');
			$data['test_rich_baidu'] = input('post.test_rich_baidu');
			$data['test_url'] = input('post.test_url');

			$insert = Db::table('bew_z_test')->insert($data);
			if(empty($insert)){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	public function test_edit(){
		if(Request::isPost()){
			$test_id = input('post.test_id');
			$data['test_input'] = input('post.test_input');
			$data['test_reference'] = input('post.test_reference');
			$data['test_time'] = input('post.test_time');
			if(!empty($data['test_time'])){
				$data['test_time'] = strtotime($data['test_time']);
			}
			$data['test_data'] = input('post.test_data');
			$data['test_datatime'] = input('post.test_datatime');
			$data['test_img'] = input('post.test_img');
			$data['test_rich'] = input('post.test_rich');
			$data['test_rich_baidu'] = input('post.test_rich_baidu');
			$data['test_url'] = input('post.test_url');

			$update = Db::table('bew_z_test')->where('test_id',$test_id)->update($data);
			if(empty($update)){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$test_id = input('get.test_id');
			$find = Db::table('bew_z_test')->where('test_id',$test_id)->find();
			if(!empty($find)){
				$find['test_time'] = date('Y-m-d H:i:s',$find['test_time']);
			}
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
	public function test_del(){
		$test_id = (int)input('post.test_id');
		$res = Db::table('bew_z_test')->where('test_id',$test_id)->delete();
		if(empty($res)){
			$this->returnCode('91000003');
		}
		$this->returnCode(0);
	}
	public function test_static_list(){
		$lists = Db::table('bew_z_test')
					->order('test_id DESC')
					->paginate();
		View::assign([
			'lists' => $lists
		]);
		return View::fetch();
	}
	public function test_static_add(){
		if(Request::isPost()){
			$data['test_input'] = input('post.test_input');
			$data['test_reference'] = input('post.test_reference');
			$data['test_time'] = input('post.test_time');
			if(!empty($data['test_time'])){
				$data['test_time'] = strtotime($data['test_time']);
			}
			$data['test_data'] = input('post.test_data');
			$data['test_datatime'] = input('post.test_datatime');
			$data['test_img'] = input('post.test_img');
			$data['test_rich_baidu'] = input('post.test_rich_baidu');
			$data['test_url'] = input('post.test_url');

			$insert = Db::table('bew_z_test')->insert($data);
			if(empty($insert)){
				$this->returnCode('91000001');
			}
			$this->returnCode(0);
		}else{
			return View::fetch();
		}
	}
	public function test_static_edit(){
		if(Request::isPost()){
			$test_id = input('post.test_id');
			$data['test_input'] = input('post.test_input');
			$data['test_reference'] = input('post.test_reference');
			$data['test_time'] = input('post.test_time');
			if(!empty($data['test_time'])){
				$data['test_time'] = strtotime($data['test_time']);
			}
			$data['test_data'] = input('post.test_data');
			$data['test_datatime'] = input('post.test_datatime');
			$data['test_img'] = input('post.test_img');
			$data['test_rich'] = input('post.test_rich');
			$data['test_rich_baidu'] = input('post.test_rich_baidu');
			$data['test_url'] = input('post.test_url');

			$update = Db::table('bew_z_test')->where('test_id',$test_id)->update($data);
			if(empty($update)){
				$this->returnCode('91000002');
			}
			$this->returnCode(0);
		}else{
			$test_id = input('get.test_id');
			$find = Db::table('bew_z_test')->where('test_id',$test_id)->find();
			if(!empty($find)){
				$find['test_time'] = date('Y-m-d H:i:s',$find['test_time']);
				if(!empty($find['test_img'])){
					$find['test_img_s'] = explode(';',$find['test_img']);
				}
			}
			View::assign([
				'find' => $find
			]);
			return View::fetch();
		}
	}
}